package com.and9.mchen.mcmusic.util;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

/** 
* Struts2工具类. 
* 
* 实现获取Request/Response/Session的简化函数. 
* 
* 
*/ 
public class Struts2Utils { 

    /** 
     * 取得HttpSession的简化函数. 
     */ 
    public static HttpSession getSession() { 
        return ServletActionContext.getRequest().getSession(); 
    }

    /** 
     * 取得HttpSession的简化函数. 
     */ 
    public static HttpSession getSession(boolean isNew) { 
        return ServletActionContext.getRequest().getSession(isNew); 
    }

    /** 
     * 取得HttpSession中Attribute的简化函数. 
     */ 
    public static Object getSessionAttribute(String name) { 
        HttpSession session = getSession(false); 
        return (session != null ? session.getAttribute(name) : null); 
    } 
    /** 
     * 设置HttpRequest中Attribute的简化函数. 
     */ 
    public static void setRequestAttribute(String key ,String value) { 
        getRequest().setAttribute(key, value); 
    }

    /** 
     * 取得HttpRequest的简化函数. 
     */ 
    public static HttpServletRequest getRequest() { 
        return ServletActionContext.getRequest(); 
    }

    /** 
     * 取得HttpRequest中Parameter的简化方法. 
     */ 
    public static String getParameter(String name) { 
        return getRequest().getParameter(name); 
    }

    /** 
     * 取得HttpResponse的简化函数. 
     */ 
    public static HttpServletResponse getResponse() { 
        return ServletActionContext.getResponse(); 
    } 
    
    public static ServletContext getServletContext(){
    	return ServletActionContext.getServletContext();
    }
    
    public static Object getServletContextAttribute(String name){
    	ServletContext servletContext=getServletContext();
    	return (servletContext != null ? servletContext.getAttribute(name) : null);
    }
}